//
//  AXHdzqHomeVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/30.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHdzqHomeVC.h"
#import "LKCircleScrollView.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXHdzqHomeFirstCell.h"
#import "AXHdzqHomeSecondCell.h"
#import "AXHdzqHomeThirdCell.h"
#import "AXHdzqHomeFourthCell.h"
#import "AXHdzqYsVC.h"
#import "AXHdzqMsVC.h"
#import "AXHdzqPdVC.h"
#import "CFDetailInfoController.h"
#import "AXHdzqJfhdVC.h"



@interface AXHdzqHomeVC ()<UITableViewDelegate,UITableViewDataSource,LKCircleScrollViewDelegate, LKCircleScrollViewDataSource,UISearchBarDelegate>
/** headView */
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
/** 广告图片 */
@property (nonatomic ,strong) UIImageView *advImageView;
/** 循环滚动View */
@property (nonatomic, strong) LKCircleScrollView *circleScrollView;
@property (nonatomic, strong) NSArray<NSString *> *scrollViewImageNames;

@end

@implementation AXHdzqHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);//headView 的大小
    _headView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _headView;
    
}

- (void)creatView {
    
    _headView = [[UIView alloc]init];
    
    //添加scrollview
    self.scrollViewImageNames = @[@"shcm", @"sg2.jpeg.jpeg", @"sg3.jpeg.jpeg"];
    //self.circleScrollView = [[LKCircleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView = [[LKCircleScrollView alloc]init];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView.delegate = self;
    self.circleScrollView.dataSource = self;
    [self.headView addSubview:self.circleScrollView];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Wi, Hi-Hs-Htb-44) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    //注册cell
    [self.tableView registerClass:[AXHdzqHomeFirstCell class] forCellReuseIdentifier:[AXHdzqHomeFirstCell reuseID]];
    [self.tableView registerClass:[AXHdzqHomeSecondCell class] forCellReuseIdentifier:[AXHdzqHomeSecondCell reuseID]];
    [self.tableView registerClass:[AXHdzqHomeThirdCell class] forCellReuseIdentifier:[AXHdzqHomeThirdCell reuseID]];
    [self.tableView registerClass:[AXHdzqHomeFourthCell class] forCellReuseIdentifier:[AXHdzqHomeFourthCell reuseID]];
  
    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 129);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
   
    
    self.circleScrollView.sd_layout
    .leftSpaceToView(self.headView,0)
    .rightSpaceToView(self.headView,0)
    .topSpaceToView(self.headView,0)
    .heightIs(129);
    
    [_headView setupAutoHeightWithBottomView:self.circleScrollView bottomMargin:0];
    
}


#pragma mark - tableViewDelegate & tableViewDatasorce

//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return nil;
    }
    else if (section == 1){
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
        /** 图片比例切 */
        img.contentMode =UIViewContentModeScaleAspectFill;
        img.clipsToBounds=YES;//
        img.image = [UIImage imageNamed:@"shcm"];
        [view addSubview:img];
        
        UILabel *titLable = [[UILabel alloc]init];
        titLable.cornerRadius = 5;
        titLable.text = @"公司活动 ";
        titLable.font = FONT_FOURTEEN;
        titLable.backgroundColor = COLOR_ORG;
        titLable.textColor = [UIColor whiteColor];
        [img addSubview:titLable];
        
        titLable.sd_layout
        .leftSpaceToView(img, 10)
        .topSpaceToView(img, 10)
        .widthIs(60)
        .heightIs(30);
        
        return view;
        
    }
    else if (section == 2){
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        
        UILabel *titLable = [[UILabel alloc]init];
        titLable.text = @"每日必买";
        titLable.font = FONT_FOURTEEN;
        titLable.backgroundColor = [UIColor whiteColor];
        titLable.textAlignment = NSTextAlignmentCenter;
        titLable.textColor = COLOR_GREEN;
        [view addSubview:titLable];
        titLable.sd_layout
        .leftSpaceToView(view, 0)
        .centerYEqualToView(view)
        .widthIs(SCREEN_WIDTH)
        .heightIs(44);
        
        return view;
        
    }
    else if (section == 3){
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        
        UILabel *titLable = [[UILabel alloc]init];
        titLable.text = @"精选榜单";
        titLable.font = FONT_FOURTEEN;
        titLable.backgroundColor = [UIColor whiteColor];
        titLable.textAlignment = NSTextAlignmentCenter;
        titLable.textColor = COLOR_GREEN;
        [view addSubview:titLable];
        titLable.sd_layout
        .leftSpaceToView(view, 0)
        .centerYEqualToView(view)
        .widthIs(SCREEN_WIDTH)
        .heightIs(44);
        
        return view;
    }
    
    
    
    return 0;
    
    
}
/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }
    else if (section == 1) {
        
        return 80;
    }
    
    
    return 44;
    
}
/** cell 的点击方法 */

//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
         float w = (Wi - 30)/2;
        
        return ((w*2)+10);
        
    }else if (indexPath.section == 1) {
        
        return ((SCREEN_WIDTH-40)/3.0);
        
    }else if (indexPath.section == 2) {
        
        return ((SCREEN_WIDTH-40)/3.0+40);
        
    }
    
    return ((SCREEN_WIDTH-40)/3.0+20);
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        
        AXHdzqHomeFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqHomeFirstCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.tapButtonClickedBlock = ^(NSInteger index) {
            
            //全部宝贝、优先体验
            if (index == 0) {
                
                AXHdzqYsVC *vc = [[ AXHdzqYsVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index ==1) {
                
                AXHdzqMsVC *vc = [[AXHdzqMsVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
                
            }else if (index == 2){
                
                AXHdzqPdVC *vc = [[ AXHdzqPdVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            else if (index == 3){
                
                AXHdzqJfhdVC *vc = [[AXHdzqJfhdVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        };
        
        //cell 的点击
        cell.tapCelllickedBlock = ^(NSInteger index) {
            NSLog(@"vc_Cell_click");
            CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        };
        
        
        
        return cell;
        
        
    }
        
    

    /** 公司活动*/
    else if (indexPath.section == 1) {
        
        AXHdzqHomeSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqHomeSecondCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        //按钮的点击
        cell.tapButtonClickedBlock = ^(NSInteger index) {
           
        };
        //cell的点击
        
        
        
   
        return cell;
    }
    
    //每日必买
   else if (indexPath.section == 2) {
        
        AXHdzqHomeThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqHomeThirdCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        return cell;
    }
    
  
    
    
    //精选榜单
     AXHdzqHomeFourthCell *cell1 = [tableView dequeueReusableCellWithIdentifier:[ AXHdzqHomeFourthCell reuseID] forIndexPath:indexPath];
    [cell1 setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell1;
    
    
    
}



#pragma mark - Circle Scroll View Data Source & Delegate

- (NSInteger)numberOfViewsInCircleScrollView:(LKCircleScrollView *)circleScrollView {
    
    return self.scrollViewImageNames.count;
}

/** 视图来源 */
- (UIView *)circleScrollView:(LKCircleScrollView *)circleScrollView viewAtIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.scrollViewImageNames[index]]];
    imageView.contentMode =UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;//
    
    if (index == 0) {
        
        imageView.userInteractionEnabled = YES;
        
        
        
    }
    
    
    return imageView;
}

/** 点击View时调用  */
- (void)circleScrollView:(LKCircleScrollView *)circleScrollView didTapViewAtIndex:(NSInteger)index {
    if (index == 0) {
        //事件不会触发，点击事件可能被ScrollView截获
        //        ZXVoteProductionListViewController *vc = [[ZXVoteProductionListViewController alloc] init];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
}





- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


#pragma mark - UISearchBar delegate
//将要开始编辑时的回调，返回为NO，则不能编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    return NO;
}


@end



